package top.fzqblog.firstapp;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scwang.smartrefresh.header.FlyRefreshHeader;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.header.WaterDropHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.footer.FalsifyFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;
import jp.wasabeef.recyclerview.animators.SlideInLeftAnimator;
import rx.Subscriber;
import top.fzqblog.adapter.XRVAdapter;
import top.fzqblog.model.Meizi;
import top.fzqblog.model.Results;
import top.fzqblog.service.RequestService;
import top.fzqblog.util.GlideImageLoader;

/**
 * Created by 抽离 on 2017/9/27 0027.
 */

public class SmartRefreshLayoutTest extends AppCompatActivity {

    @BindView(R.id.refreshLayout)
    public SmartRefreshLayout refreshLayout;

    @BindView(R.id.smartRV)
    public RecyclerView recyclerView;

    @BindView(R.id.banner)
    public Banner banner;

    private XRVAdapter xrvAdapter;

    private int page = 1;

    private int total = 200;

    RequestService requestService = new RequestService();

    private boolean isFirst = true;

    private ArrayList<String> list;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.smart_refresh_layout);
        ButterKnife.bind(this);

        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(Arrays.asList(R.mipmap.home_one_2, R.mipmap.home_one_6, R.mipmap.home_one_10));

        //banner设置方法全部调用完毕时最后调用
        banner.start();

        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));


        refreshLayout.setRefreshHeader(new MaterialHeader(this).setShowBezierWave(true));
        //设置 Footer 为 球脉冲
        refreshLayout.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                Log.i("tag","refresh");
                getMeizi(refreshlayout);
            }
        });

        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                Log.i("tag", "加载");
                getMeizi(refreshlayout);
            }
        });

        if(isFirst){
            isFirst = false;
            refreshLayout.autoRefresh();
        }

    }


    public void getMeizi(RefreshLayout refreshLayout){
        if(page*20 >= total){
            refreshLayout.finishLoadmore();
            refreshLayout.setLoadmoreFinished(true);
        }
        requestService.getMeizi(new Subscriber<Meizi>() {
            @Override
            public void onCompleted() {

            }
            @Override
            public void onError(Throwable throwable) {
            }
            @Override
            public void onNext(Meizi meizi) {
                if(page == 1) {
                    Log.i("tag", meizi.toString());
                    list = new ArrayList<>();
                    page ++;
                    xrvAdapter = new XRVAdapter(SmartRefreshLayoutTest.this, meizi.getResults());
                    xrvAdapter.notifyDataSetChanged();
                    recyclerView.setAdapter(xrvAdapter);
                    for (Results results : meizi.getResults()) {
                        list.add(results.getUrl());
                    }
                    //设置点击查看大图
                    xrvAdapter.setOnclickItem(position -> {
                        Intent intent = new Intent(SmartRefreshLayoutTest.this, PhotoSee.class);
                        intent.putStringArrayListExtra("imgs", list);
                        intent.putExtra("position", position);
                        startActivity(intent);
                    });
                    refreshLayout.finishRefresh();
                }else{
                    page ++;
                    xrvAdapter.addAll(meizi.getResults());
                    xrvAdapter.notifyDataSetChanged();
                    for (Results results : meizi.getResults()) {
                        list.add(results.getUrl());
                    }
                    //设置点击查看大图
                    xrvAdapter.setOnclickItem(position -> {
                        Intent intent = new Intent(SmartRefreshLayoutTest.this, PhotoSee.class);
                        intent.putStringArrayListExtra("imgs", list);
                        intent.putExtra("position", position);
                        startActivity(intent);
                    });
                    refreshLayout.finishLoadmore();
                }
            }
        }, "福利", 20, page);
    }

    public class QuickAdapter extends BaseQuickAdapter<Meizi, BaseViewHolder> {
        public QuickAdapter() {
            super(R.layout.retrofit_glide);
        }

        @Override
        protected void convert(BaseViewHolder viewHolder, Meizi item) {

        }
    }
}
